package com.baoyouqun.service.impl;

import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baoyouqun.base.utils.ConvertUtils;
import com.baoyouqun.domain.CreateCmd.ResourceTypeCreateCmd;
import com.baoyouqun.domain.Query.ResourceTypeQuery;
import com.baoyouqun.domain.UpdateCmd.ResourceTypeUpdateCmd;
import com.baoyouqun.domain.VO.ResourceTypeVO;
import com.baoyouqun.entity.ResourceType;
import com.baoyouqun.mapper.ResourceTypeMapper;
import com.baoyouqun.service.ResourceTypeService;
import jakarta.annotation.Resource;
import org.apache.poi.util.StringUtil;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;


@Service
public class ResourceTypeServiceImpl extends ServiceImpl<com.baoyouqun.mapper.ResourceTypeMapper, ResourceType> implements ResourceTypeService {

    @Resource
    private ResourceTypeMapper ResourceTypeMapper;

    @Override
    public SingleResponse<ResourceTypeVO> create(ResourceTypeCreateCmd cmd) {
        ResourceType entity =
                ConvertUtils.copyBean(cmd, ResourceType.class);
        Boolean result = save(entity);
        if (!result) {
            return SingleResponse.buildFailure("100101", "创建失败");
        }

        ResourceTypeVO vo =
                ConvertUtils.copyBean(entity, ResourceTypeVO.class);
        return SingleResponse.of(vo);
    }

    @Override
    public Response update(ResourceTypeUpdateCmd cmd) {
        if (Objects.isNull(cmd)) {
            return Response.buildFailure("100301", "参数不能为空");
        }
        if (StringUtil.isBlank(cmd.getId())) {
            return Response.buildFailure("100302", "ID不能为空");
        }
        ResourceType oldEntity = getById(cmd.getId());
        if (Objects.isNull(oldEntity)) {
            return Response.buildFailure("100303", "未查询到此数据");
        }
        oldEntity = ConvertUtils.copyBean(cmd, ResourceType.class);
        Boolean result = updateById(oldEntity);
        if (result) {
            return Response.buildSuccess();
        } else {
            return Response.buildFailure("100304", "更新失败");
        }
    }

    @Override
    public ResourceTypeVO selectById(String id) {
        ResourceType entity = baseMapper.selectById(id);
        ResourceTypeVO vo =
                ConvertUtils.copyBean(entity, ResourceTypeVO.class);
        return vo;
    }


    @Override
    public IPage<ResourceTypeVO> pageVo(ResourceTypeQuery query) {
        IPage<ResourceType> page = new Page<ResourceType>(query.getCurrent(), query.getSize());
        page = page(page, new LambdaQueryWrapper<ResourceType>());
        Page<ResourceTypeVO> voPage = new Page<>(page.getCurrent(), page.getSize(), page.getTotal());
        List<ResourceTypeVO> records = ConvertUtils.copyBeanList(page.getRecords(), ResourceTypeVO.class);
        voPage.setRecords(records);
        return voPage;
    }
}